package com.jamie.storage.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jamie.api.storage.StorageService;
import com.jamie.storage.domain.Storage;
import com.jamie.storage.mapper.StorageMapper;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Jamie
 * @date 2020/4/13
 */
@RestController
public class StorageServiceImpl implements StorageService {
    @Resource
    private StorageMapper storageMapper;

    @Override
    public void deduct(String commodityCode, int count) {
        LambdaUpdateWrapper<Storage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.ge(Storage::getCount, count);
        wrapper.eq(Storage::getCommodityCode, commodityCode);
        wrapper.setSql("count = count - " + count);
        int updateCount = storageMapper.update(null, wrapper);
        if (updateCount == 0) {
            throw new RuntimeException("库存不足，扣减失败");
        }
    }
}
